<template>
	<el-form :model="formData" :rules="formRules" ref="formRef" label-width="90px" label-position="left">
		<el-form-item label="名　　称：" prop="unitName">
			<el-input v-model="formData.unitName" @keyup.enter.native="enterKey($event, 'L1')"></el-input>
		</el-form-item>
		<el-form-item label="单位类别：" prop="unitType">
			<el-select v-model="formData.unitType"  ref="L1" placeholder="选择单位类别" style="width: 100%;" @keyup.enter.native="save('formRef')">
                <el-option v-for="item in unitArr" :label="item['name']" :value="String(item['id'])" :key="item['id']"></el-option>
            </el-select>
		</el-form-item>
		<el-form-item label="连续增加：" style="margin-bottom: 10px !important;" v-if="!rowId">
            <el-checkbox v-model="continueAdd"></el-checkbox>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" size="small" @click="save('formRef')">保存</el-button>
            <el-button type="danger" size="small" @click="close">关闭</el-button>
        </el-form-item>
	</el-form>
</template>
<script>
export default {
	data() {
		return {
			continueAdd: false,		//是否连续新增
			formData: {},
			formRules: {
				unitName: [{required: true, message: '名称不能为空', triggle: 'blur'}],
				unitType: [{required: true, message: '单位类别不能为空', triggle: 'change'}]
			},
			unitArr: [
				{
					id: 0,
					name: '普通单位'
				},
				{
					id: 1,
					name: '组合单位'
				}
			],
			rowId: this.$store.state.meterUnitStore.rowId
		}
	},
	methods: {
		getUpdateData() {			//获取更新数据
			this.$http.get('measureUnit/selectGroupInfo?id=' + this.rowId).then(res => {
				if(res.data.code == 20001) {
					if(res.data.data) {
						this.formData = res.data.data;
						this.formData['unitType'] = this.formData['unitType'].toString();
					}
				}
			});
		},
		save(formName) {		//保存
			this.$refs[formName].validate((valid) => {
                if (valid) {
                	let data = {
            			unitName: this.formData['unitName'],
            			unitType: Number(this.formData['unitType'])
            		}
                	if(this.formData['id']) {
                		//修改
                		data['id'] = this.formData['id'];
                		data['state'] = this.formData['state'];
                		this.$http({
                            method: "post",
                            url: "measureUnit/updateGroup",
                            params: data
                        }).then(res => {
                        	if(res.data.code == 20001) {
                        		this.$message({message: '修改成功', type: 'success', duration: 1500});
                        		setTimeout(() => {
                        			this.$store.dispatch('meterUnitStore/getUnitGroupTableData');
                        			this.close();
                        		}, 1000);
                        	}
                        });
                	} else {
                		//新增
                		this.$http({
                            method: "post",
                            url: "measureUnit/insertGroup",
                            params: data
                        }).then(res => {
                        	if(res.data.code == 20001) {
                        		this.$message({message: '新增成功', type: 'success', duration: 1500});
                        		setTimeout(() => {
                        			this.continueAdd ? this.$refs[formName].resetFields() : this.close();
                        			this.$store.dispatch('meterUnitStore/getUnitGroupTableData');
                        		}, 1000);
                        	}
                        });
                	}
                }
            });
		},
		close() {           //关闭弹窗
            let index = parent.layer.getFrameIndex(window.name);    //先得到当前iframe层的索引
            parent.layer.close(index);
        },
        enterKey(lastRef, nextRef) {		//回车键换行
            if(lastRef.target.value) {
                this.$refs[nextRef].focus();
            }
        }
	},
	mounted() {
		if(this.rowId) {
			this.getUpdateData();
		}
	}
}
</script>